#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
typedef struct
{
    char name[18];
    int age;
    char addr[64];
} STUDENT;


int main(int argc, char **argv)
{
    char filename[64] = "fwrite3.data";

    STUDENT stu;
    FILE *output = fopen(filename,"w+");
    if (output == NULL)
    {
        perror("file is write error");
    }

    for (size_t i = 0; i < 20; i++)
    {
        sprintf(stu.name,"student name-%ld",i);
        stu.age = 10+i;
        sprintf(stu.addr,"addre-%ld",i);
        fwrite(&stu,sizeof(stu),1,output);
    }
    fclose(output);
    memset(&stu,0,sizeof(stu));
    
    output = fopen(filename,"r+");
    if (output == NULL)
    {
        perror("file is read error");
    }
    int resu = fseek(output,10*sizeof(stu),SEEK_SET);
    // printf("resu = %d\n",resu);
    // printf("file is %d\n",feof(output));
    // if (feof(output))
    // {
    //     perror("file is eof");
    //     exit(0);
    // }
    
    int readsize = fread(&stu,sizeof(stu),1,output);
    if (readsize == 0)
    {
        perror("not read data");
        exit(1);
    }
    
    // printf("read byte is %d\n",readsize);
    printf("name=%s, age=%d, address=%s \n",stu.name,stu.age,stu.addr);
    fclose(output);


    return 0;
}